<template>
  <el-container class="layout-container">
    <!-- 顶部菜单 -->
    <el-header>
      <div class="el-header__logo"></div>
      <div class="menu-container">
        <el-menu
            active-text-color="#1890ff"
            background-color="#fff"
            text-color="#333"
            mode="horizontal"
            router
            class="custom-menu"
        >
          <el-menu-item index="/workBench/IntermediaryIndex">
            <el-icon><Management /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/buyHouses/BuyHousesIntlist">
            <el-icon><Bell /></el-icon>
            <span>卖房</span>
          </el-menu-item>
          <el-menu-item index="/RentingHouses/RentingHousesIntlist">
            <el-icon><OfficeBuilding /></el-icon>
            <span>出租</span>
          </el-menu-item>
          <el-menu-item index="/userInformation/CustomerInfo">
            <el-icon><Avatar /></el-icon>
            <span>客户信息</span>
          </el-menu-item>
          <el-menu-item index="/userInformation/LandlordInfo">
            <el-icon><Avatar /></el-icon>
            <span>房东信息</span>
          </el-menu-item>
          <el-menu-item index="/contractDelegation/ContractelegationList">
            <el-icon><Message /></el-icon>
            <span>看房委托</span>
          </el-menu-item>
          <el-menu-item index="/financialManagement/FinancialManagementIntlist">
            <el-icon><Money /></el-icon>
            <span>业绩</span>
          </el-menu-item>
          <el-menu-item index="/noticeManagement/NoticeManagementIntlist">
            <el-icon><Money /></el-icon>
            <span>通知</span>
          </el-menu-item>
          <el-sub-menu>
            <template #title>
              <el-icon><UserFilled /></el-icon>
              <span>个人中心</span>
            </template>
            <el-menu-item index="/userInt/UserInfoInt">
              <el-icon><User /></el-icon>
              <span>基本资料</span>
            </el-menu-item>
            <el-menu-item index="/userInt/UserAvatarInt">
              <el-icon><Crop /></el-icon>
              <span>更换头像</span>
            </el-menu-item>
            <el-menu-item index="/userInt/UserResetPasswordInt">
              <el-icon><EditPen /></el-icon>
              <span>重置密码</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </div>
      <div class="right-wrapper">
        <div>中介：<strong>{{ userInfoStore.info.nickname }}</strong></div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar :src="userInfoStore.info.userPic || avatar" />
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="UserInfoInt" :icon="User">基本资料</el-dropdown-item>
              <el-dropdown-item command="UserAvatarInt" :icon="Crop">更换头像</el-dropdown-item>
              <el-dropdown-item command="UserResetPasswordInt" :icon="EditPen">重置密码</el-dropdown-item>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    <el-main>
      <router-view></router-view>
    </el-main>
    <el-footer>慧家房产中介系统 ©2025 Created by 栗子</el-footer>
  </el-container>
</template>

<script setup>
import {
  Message,
  Management,
  UserFilled,
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom,
  House,
  SoldOut,
  OfficeBuilding,
  Avatar,
  Bell,
  Money,
  DocumentCopy
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { userInfoGetService } from '@/api/user.js'
import { useUserInfoStore } from '@/stores/userinfo.js'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useTokenStore } from '@/stores/token.js'

const userInfoStore = useUserInfoStore()
const router = useRouter()
const tokenStore = useTokenStore()

// 获取个人信息
const getUserInf = async () => {
  const result = await userInfoGetService()
  userInfoStore.setInfo(result.res)
}
getUserInf()

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm(
        '确认退出登录？',
        '提示',
        { type: 'warning' }
    ).then(() => {
      userInfoStore.removeInfo()
      tokenStore.removeToken()
      router.push('/')
      ElMessage.success('退出成功')
    }).catch(() => ElMessage.info('已取消'))
  } else {
    router.push(`/userInt/${command}`)
    console.log(`/userInt/${command}`)
  }
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  background-color: #f5f5f5;

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    padding: 0 20px;
    justify-content: space-between;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

    &__logo {
      width: 120px;
      height: 100%;
      background: url('@/assets/logoTop.png') no-repeat center / 120px auto;
    }

    .menu-container {
      flex: 1;
      min-width: 0;
      overflow: visible;
    }

    .custom-menu {
      border-bottom: none;
      flex-shrink: 0;

      .el-menu-item,
      .el-sub-menu__title {
        white-space: nowrap;
        padding: 0 30px !important;
      }
    }

    .right-wrapper {
      display: flex;
      align-items: center;
      gap: 20px;
    }

    .el-dropdown__box {
      display: flex;
      align-items: center;
      color: #333;

      .el-icon {
        margin-left: 10px;
      }
    }
  }

  .el-main {
    padding: 20px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    margin: 20px;
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
    border-top: 1px solid #eee;
  }
}
</style>
